# _*_ coding: utf-8 _*_
from odoo import models, fields, api, Command


class MailActivityType(models.Model):
    _inherit = "mail.activity.type"

    wecom_automation_on_create = fields.Many2one('base.automation')
    wecom_automation_on_timeout = fields.Many2one('base.automation')
    wecom_automation_on_24_hours = fields.Many2one('base.automation')

    def action_quick_add_wecom_on_create_automation(self):
        self.ensure_one()
        self.wecom_automation_on_create = self.env['base.automation'].create({
            'name': "Automation On Activity Create",
            'model_id': self.env['ir.model'].search([('model', '=', 'mail.activity')]).id,
            'trigger': 'on_create',
            'filter_pre_domain': f'[("activity_type_id", "=", {self.id})]',
            'action_server_ids': [Command.create({
                'name': "xxx",
                'model_id': self.env['ir.model'].search([('model', '=', 'mail.activity')]).id,
                'state': 'wecom',
            })],
        })

    def action_quick_add_wecom_timeout_automation(self):
        self.ensure_one()
        self.wecom_automation_on_timeout = self.env['base.automation'].create({
            'name': "Automation On Activity Timeout",
            'model_id': self.env['ir.model'].search([('model', '=', 'mail.activity')]).id,
            'trigger': 'on_time',
            'trg_date_id': self.env['ir.model.fields'].search([
                ('model_id.model', '=', 'mail.activity'),
                ('name', '=', 'date_deadline'),
            ]).id,
            'trg_date_range': 1,
            'trg_date_range_type': 'hour',
            'filter_pre_domain': f'[("activity_type_id", "=", {self.id}), ("date_done", "=", False)]',
            'action_server_ids': [Command.create({
                'name': "Automation On Activity Timeout",
                'model_id': self.env['ir.model'].search([('model', '=', 'mail.activity')]).id,
                'state': 'wecom',
            })],
        })

    def action_quick_add_wecom_24_hours_automation(self):
        self.ensure_one()
        self.wecom_automation_on_24_hours = self.env['base.automation'].create({
            'name': "Automation Activity Before 24 Hours",
            'model_id': self.env['ir.model'].search([('model', '=', 'mail.activity')]).id,
            'trigger': 'on_time',
            'trg_date_id': self.env['ir.model.fields'].search([
                ('model_id.model', '=', 'mail.activity'),
                ('name', '=', 'date_deadline'),
            ]).id,
            'trg_date_range': -24,
            'trg_date_range_type': 'hour',
            'filter_pre_domain': f'[("activity_type_id", "=", {self.id}), ("date_done", "=", False)]',
            'action_server_ids': [Command.create({
                'name': "Automation Activity Before 24 Hours",
                'model_id': self.env['ir.model'].search([('model', '=', 'mail.activity')]).id,
                'state': 'wecom',
            })],
        })
